<template>
    <div class="admin">
        <el-card class="!border-none" shadow="never">
            <el-form class="mb-[-16px]" :model="formData" inline>
                <el-form-item label="所属企业">
                    <el-select class="w-[280px]" v-model="formData.ac_id">
                        <el-option label="全部" value="" />
                        <el-option
                            v-for="(item, index) in optionsData.companyList"
                            :key="index"
                            :label="item.c_name"
                            :value="item.ac_id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="用户名称">
                    <el-input
                        v-model="formData.user"
                        class="w-[280px]"
                        clearable
                        placeholder="用户名称"
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="机器人">
                    <el-input
                        v-model="formData.agent_name"
                        class="w-[280px]"
                        clearable
                        placeholder="对话的机器人名称"
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="关键词">
                    <el-input
                        v-model="formData.query"
                        class="w-[280px]"
                        clearable
                        placeholder="请输入关键词"
                        @keyup.enter="resetPage"
                    />
                </el-form-item>

                <el-form-item label="提问时间">
                    <daterange-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
            <!-- <el-button v-perms="['company.staff/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" size="large">
                    <el-table-column label="ID" prop="id" min-width="60" />
                    <el-table-column label="用户信息" prop="user" min-width="100" />
                    <el-table-column
                        label="提问时间"
                        prop="create_time"
                        min-width="220"
                        show-tooltip-when-overflow
                    />
                    <el-table-column
                        label="更新时间"
                        prop="create_time"
                        min-width="220"
                        show-tooltip-when-overflow
                    />
                    <el-table-column label="用户提问" prop="query" min-width="140" />
                    <el-table-column label="所属智能体" prop="agent_name" min-width="140" />
                    <el-table-column
                        label="所属会话"
                        prop="conversation_id"
                        min-width="140"
                        show-tooltip-when-overflow
                    />
                    <el-table-column
                        label="所属企业"
                        prop="company_name"
                        min-width="300"
                        show-tooltip-when-overflow
                    />
                    <el-table-column label="操作" width="140" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['company.staff/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                查看会话
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="admin">
import { adminEdit, adminLists, adminDelete, adminUpdStatus } from '@/api/question'
import { companyListAll } from '@/api/company'
import { useDictOptions } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 表单数据
const formData = reactive({
    ac_id: '',
    agent_id: '',
    query: '',
    user: '',
    agent_name:'',
    start_time: '',
    end_time: '',
    type:'1'
})
const queryParams = reactive({
    ac_id: '',
    agent_name: '',
    query: '',
    user: '',
    start_time: '',
    end_time: '',
    type:'1'
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: adminLists,
    params: formData
})

const changeStatus = (data: any) => {
    adminUpdStatus({
        id: data.id,
        disable: data.disable
    }).finally(() => {
        getLists()
    })
    adminEdit({
        id: data.id,
        account: data.account,
        name: data.name,
        role_id: data.role_id,
        disable: data.disable,
        multipoint_login: data.multipoint_login
    }).finally(() => {
        getLists()
    })
}
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

const { optionsData } = useDictOptions<{
    companyList: any[]
}>({
    companyList: {
        api: companyListAll
    }
})

onMounted(() => {
    getLists()
})
</script>
